//
//  BaseStockChildListController.m
//  Stock
//
//  Created by blacksun on 11-6-24.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "BaseStockChildListController.h"


@implementation BaseStockChildListController
@synthesize stockNewses;
@synthesize sectionId;
@synthesize sectionIndex;
@synthesize readStatus;
@synthesize service;
//@synthesize tableView;

#pragma mark -Service Delegate
-(void)requestFinishedWithResult:(id)aResult andUserinfo:(NSDictionary *)aUserinfo{
    StockNews *lastedNews = [aResult lastObject];
    currentIndex = [lastedNews.sectionIndex intValue];
}
-(void)requestFinishedwithError:(NSError *)aError andUserinfo:(NSDictionary *)aUserinfo{
    
}
-(void)queryStocknewses{
    
}

#pragma mark -dealloc
- (void)dealloc
{
    
    [self.service batchAndAsynchronousSaveObjects:readStatus];
    self.stockNewses = nil;
    self.readStatus = nil;
    self.sectionId = nil;
    self.service = nil;
    //self.tableView = nil;
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.readStatus = [NSMutableArray array];
    self.service = [[[StockNewsService alloc] initWithDelegate:self] autorelease];
    self.tableView.rowHeight = 60.0;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = [UIColor clearColor];
    
    self.tableView.tableFooterView = [[StockAppHelper shareInstance] getTableFooterView];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    [self.service batchAndAsynchronousSaveObjects:readStatus];
    self.service = nil;
    self.readStatus = nil;
    self.stockNewses = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [stockNewses count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"StockNewsCell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(30.0, 5.0, 280.0, 30.0)];
        titleLabel.backgroundColor = [UIColor clearColor];
        titleLabel.tag = kStockNewsTitleTag;
        titleLabel.textColor = [UIColor whiteColor];
        UILabel *sourceLabel = [[UILabel alloc] initWithFrame:CGRectMake(30.0, 35.0, 170.0, 20.0)];
        sourceLabel.backgroundColor = [UIColor clearColor];
        sourceLabel.tag = kStockNewsSourceTag;
        sourceLabel.textColor = [UIColor grayColor];
        sourceLabel.font = [UIFont systemFontOfSize:14.0];
        //pubdateLabel.textAlignment = UITextAlignmentCenter;
        UILabel *pubdateLabel = [[UILabel alloc] initWithFrame:CGRectMake(210.0, 35.0, 100.0, 20.0)];
        pubdateLabel.backgroundColor = [UIColor clearColor];
        pubdateLabel.tag = kStockNewsPubdateTag;
        pubdateLabel.font = [UIFont systemFontOfSize:14.0];
        pubdateLabel.textColor = [UIColor grayColor];
        //sourceLabel.textAlignment = UITextAlignmentCenter;
        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(5.0, 20.0, 15.0, 15.0)];
        imageView.image = [UIImage imageNamed:@"unread.png"];
        imageView.tag = kStockNewsUnreadTag;
        
        [cell.contentView addSubview:titleLabel];
        [cell.contentView addSubview:pubdateLabel];
        [cell.contentView addSubview:sourceLabel];
        [cell.contentView addSubview:imageView];
        
        [titleLabel release];
        [pubdateLabel release];
        [sourceLabel release];
        [imageView release];
    }
    UILabel *titleLabel = (UILabel *)[cell viewWithTag:kStockNewsTitleTag];
    UILabel *pubdateLabel = (UILabel *)[cell viewWithTag:kStockNewsPubdateTag];
    UILabel *sourceLabel = (UILabel *)[cell viewWithTag:kStockNewsSourceTag];
    UIImageView *unreadImageView = (UIImageView *)[cell viewWithTag:kStockNewsUnreadTag];
    
    NSUInteger index = indexPath.row;
    StockNews *currentNews = [stockNewses objectAtIndex:index];
    titleLabel.text = currentNews.title;
    pubdateLabel.text = currentNews.pubDate;
    sourceLabel.text = currentNews.source;
    if (currentNews.readed) {
        unreadImageView.hidden = YES;
    }else{
        unreadImageView.hidden = NO;
    }
    //NSLog(@"%@",currentNews.reportId);
    NSString *imageName = @"cellbg1.png";
    if (index%2==1) {
        imageName = @"cellbg2.png";
    }
    cell.backgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]] autorelease];
    
    return cell;
}

#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    StockNews *stockNews = [self.stockNewses objectAtIndex:indexPath.row];
    if (!stockNews.readed) {
        stockNews.readed = YES;
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        UIImageView *unreadImageView = (UIImageView *)[cell viewWithTag:kStockNewsUnreadTag];
        unreadImageView.hidden = YES;
    }
    
    StockNewsDetailController *detailController = [[StockNewsDetailController alloc] initWithNibName:@"StockNewsDetailController" bundle:nil];
    detailController.stockNews = stockNews;
    detailController.parent = self;
    
    StockAppDelegate *delegate = [UIApplication sharedApplication].delegate;
    [delegate.navigationController.topViewController.navigationController pushViewController:detailController animated:YES];
    [detailController release];
}

@end
